{*******************************************************************************
* The contents of this file are used with permission, subject to the Mozilla   *
* Public License Version 1.1 (the "License"); you may not use this file except *
* in compliance with the License. You may obtain a copy of the License at      *
* http://www.mozilla.org/MPL/                                                  *
*                                                                              *
* Software distributed under the License is distributed on an "AS IS" basis,   *
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for *
* the specific language governing rights and limitations under the License.    *
*                                                                              *
* The Original Code is distributed as part of the "mediate" product and is     *
* Copyright (C) @home media limited. All Rights Reserved.                      *
*                                                                              *
* Email: support@athomemedia.co.uk                                             *
* Web:   http://www.athomemedia.co.uk                                          *
*******************************************************************************}

unit MediaFolderManager;

interface

uses MediaLocations, MediaRepository;

type
  TAHMMediaFolderManager = class
  private
    FConfigPath: String;
    FThumbPath: String;
    FModified: Boolean;
    FImportReqd: Boolean;
    FRepository: TAHMMediaRepository;
    FLocations: TAHMMediaLocationParser;
  protected
    procedure SetModified(Modified: Boolean);
  public
    constructor Create;
    destructor Destroy; override;
    property ConfigPath: String read FConfigPath;
    property ThumbnailPath: String read FThumbPath;
    property Modified: Boolean read FModified write SetModified;
    property ImportRequired: Boolean read FImportReqd write FImportReqd;
    property Repository: TAHMMediaRepository read FRepository;
    property Locations: TAHMMediaLocationParser read FLocations;
    procedure LoadLocations;
    procedure SaveLocations;
    procedure CreateDatabase;
    function OpenDatabase: Boolean;
    function SaveDatabase: Boolean;
    procedure CloseDatabase;
    procedure CloseMediate;
  end;

implementation

uses Classes, SysUtils, Dialogs, AHMConsts, MediaSchema, Messaging, Utilities;

constructor TAHMMediaFolderManager.Create;
var
  AppHome: String;
begin
  inherited Create;

  FLocations := TAHMMediaLocationParser.Create;

  // Set path for required application files
  AppHome := ParentFolder(ApplicationPath);
  FConfigPath := AppHome + CONFIG_FOLDER + PathDelim;
  FThumbPath := AppHome + THUMBS_FOLDER + PathDelim;

  // Check for a missing media database
  FImportReqd := not TAHMMediaRepository.DatabaseExists(FConfigPath);
end;

destructor TAHMMediaFolderManager.Destroy;
begin
  FLocations.Free;

  inherited Destroy;
end;

procedure TAHMMediaFolderManager.SetModified(Modified: Boolean);
begin
  FModified := Modified;
  if Modified then FImportReqd := True;
end;

procedure TAHMMediaFolderManager.LoadLocations;
begin
  // Parse XML to populate media locations
  try
    FLocations.ParseXML(FConfigPath);
  except
    on E:Exception do
      MessageDlg('An error was encountered whilst loading your media directories ' +
                 'configuration. This may be because the configuration file was ' +
                 'modified manually - for more information please contact @home ' +
                 'media support and report the following error message: ' +
                 sLineBreak + sLineBreak + E.Message, mtError, [mbOK], 0);
  end;
  FModified := False;
end;

procedure TAHMMediaFolderManager.SaveLocations;
var
  Config: TStringList;
begin
  Config := TStringList.Create;
  try
    FLocations.SaveAsXML(Config);
    Config.SaveToFile(FConfigPath + MEDIALOC_FILENAME);
    FModified := False;
  finally
    Config.Free;
  end;
end;

procedure TAHMMediaFolderManager.CreateDatabase;
begin
  // Create empty database structure in temp file
  FRepository := TAHMMediaRepository.Create;
  FRepository.CreateTemporaryFile(FConfigPath);

  // Configure music folders for import
  FRepository.FolderList := FLocations.MusicFolders;
  FRepository.ThumbnailPath:= FThumbPath;
end;

function TAHMMediaFolderManager.OpenDatabase: Boolean;
begin
  // Open existing media database
  FRepository := TAHMMediaRepository.Create;
  Result := FRepository.OpenExistingFile(FConfigPath);

  // Configure thumbnail folder for updates
  FRepository.ThumbnailPath:= FThumbPath;
end;

function TAHMMediaFolderManager.SaveDatabase: Boolean;
begin
  // Attempt to overwrite live db with our newly built one
  Result := FRepository.PromoteTemporaryFile(FConfigPath);
  if Result then FImportReqd := False;
end;

procedure TAHMMediaFolderManager.CloseDatabase;
begin
  FreeAndNil(FRepository);
end;

procedure TAHMMediaFolderManager.CloseMediate;
begin
  // Send shutdown message to mediate and give it a chance to exit
  MessengerInstance.RequestMediateShutdown;
  Sleep(1000);
end;

end.
